#!/usr/bin/env bash

jruby_install_build_nailgun()
{
  __rvm_cd "${rvm_src_path}/$rvm_ruby_string/tool/nailgun" &&
  case "$rvm_ruby_version" in
    1.2*|1.3*)
      true
      ;;
    *)
      env "${rvm_configure_env[@]}" ./configure --prefix=$rvm_ruby_home
      ;;
  esac &&
  __rvm_make "${rvm_make_flags[@]}"
}

jruby_install_ant()
{
  __rvm_log_command "ant.jar" "$rvm_ruby_string - #ant jar" __rvm_ant jar || return $?

  [[ -f "${rvm_src_path}/$rvm_ruby_string/bin/jruby" ]] ||
  {
    result=$?
    rvm_error "JRuby reported it was installed successfully, but RVM could not find proper installation,
please check why '${rvm_src_path}/$rvm_ruby_string/bin/jruby' was not created,
maybe here '$rvm_log_path/$rvm_ruby_string/ant.jar.log',
also make sure to report an error here 'https://github.com/jruby/jruby/issues'
about not returning non zero status in case of errors."
    return $result
  }

  if
    (( ${#rvm_configure_flags[@]} ))
  then
    __rvm_log_command "ant.jar.flags" "$rvm_ruby_string - #ant jar ${rvm_configure_flags[*]}" \
      __rvm_ant jar "${rvm_configure_flags[@]}" || return $?
  fi

  __rvm_log_command "ant.cext" "$rvm_ruby_string - #ant cext" __rvm_ant cext "${rvm_configure_flags[@]}" ||
  rvm_warn "cext is known to fail please report here: https://jira.codehaus.org/browse/JRUBY"

  __rvm_version_compare "$rvm_ruby_version" -ge "1.7.0" ||
  __rvm_log_command "nailgun" "$rvm_ruby_string - #nailgun" jruby_install_build_nailgun ||
  return $?
}

jruby_install_mvn_command()
{
  __rvm_log_command "$mvn_label" "$rvm_ruby_string - #$mvn_label" "${mvn_command[@]}" || return $?
}

jruby_install_mvn()
{
  \typeset -a mvn_command
  \typeset mvn_label
  mvn_command=( "${1:-mvn}" )
  mvn_label="{1:-mvn}"
  mvn_label="${mvn_command##*/}"

  jruby_install_mvn_command || return $?
  if
    (( ${#rvm_configure_flags[@]} ))
  then
    mvn_command+=( "${rvm_configure_flags[@]}" )
    mvn_label+="_flags"
    jruby_install_mvn_command || return $?
  fi
}

jruby_install_mvnw()
{
  jruby_install_mvn "${rvm_src_path}/$rvm_ruby_string/mvnw" || return $?
}

jruby_install_check_java()
{
  builtin command -v java > /dev/null ||
  {
    printf "%b" "java must be installed and in PATH for JRuby."
    return 1
  }
}

jruby_install_check_jruby_home()
{
  if
    [[ -n "$JRUBY_HOME" ]]
  then
    printf "%b" "You have environment variable JRUBY_HOME set, please unset it before installing/using JRuby."
    return 2
  fi
}

jruby_install_check_java_home()
{
  if
    [[ -z "${JAVA_HOME:-}" ]]
  then
    export JAVA_HOME="$(
      cd "${rvm_scripts_path}/extras/java_read_properties"
      java ReadProperties java.home
    )"
    if
      [[ -n "$JAVA_HOME" &&
        -x "$JAVA_HOME/../bin/javac" &&
        -f "$JAVA_HOME/../lib/tools.jar"
      ]]
    then
      rvm_debug "Detected JAVA_HOME=$JAVA_HOME has JDK level up"
      JAVA_HOME="${JAVA_HOME%/*}" # cd ..
    fi
    rvm_warn "\$JAVA_HOME was empty, setting up JAVA_HOME=$JAVA_HOME, if it fails try setting JAVA_HOME to something sensible and try again."
  fi
}

jruby_install()
{
  __rvm_setup_compile_environment "${rvm_ruby_string}" || return $?

  jruby_install_check_java || return $?
  jruby_install_check_jruby_home || return $?
  jruby_install_check_java_home

  __rvm_cd "${rvm_src_path}"

  rvm_ruby_tag="${rvm_ruby_version}"
  __rvm_fetch_ruby || return $?

  __rvm_cd "${rvm_src_path}/$rvm_ruby_string"

  __rvm_apply_patches

  \typeset method
  method=$( jruby_installation_method  "${rvm_ruby_string}" "${rvm_ruby_version}" )  || return $?
  jruby_install_$method  || return $?

  __rvm_rm_rf "$rvm_ruby_home" && \command \mkdir -p "$rvm_ruby_home" &&
  __rvm_log_command "install" "$rvm_ruby_string - #installing to $rvm_ruby_home" \
    __rvm_cp -Rf "${rvm_src_path}/$rvm_ruby_string"/{bin,lib} "$rvm_ruby_home"/ || return $?

  case ${rvm_ruby_string} in
    jruby-head*|jruby-1.7.*)
      (
        __rvm_cd "$rvm_ruby_home/bin/"
        for binary in irb ruby gem
        do
          [[ -f "${binary}" ]] || ln -fs "j${binary}" "${binary}"
        done
      )
      ;;
    *)
      (
        __rvm_cd "$rvm_ruby_home/bin/"
        for binary in jirb jruby jgem
        do
          ln -fs "$binary" "${binary#j}"
        done
      )
      ;;
  esac

  chmod +x "$rvm_ruby_home/bin/jruby"
  __rvm_initial_gemsets_create "$rvm_ruby_home/bin/ruby"

  case ${rvm_ruby_string} in
    jruby-head*|jruby-1.7.*)
      binaries=()
      ;;
    *)
      binaries=(jrubyc jirb_swing jirb jgem rdoc ri spec autospec testrb ast generate_yaml_index.rb rake)
      ;;
  esac
  __rvm_post_install
  __rvm_fetch_ruby_cleanup
}
